// cd /projects/hsieh_project/proj_201809/code_2_201910/
// qstata out_city_jobcr_city_emp.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_201910/out_city_jobcr_city_emp_log, replace text

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/201910_main/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/201910_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/city_jobcr_city_emp/"
capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

// Load functions
do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_lpolyci.do"

//==============================================================================


capture program drop fig_lpoly
program fig_lpoly
	args i_city v_y t_y png_out c_cr c_den
	/*
	i_city: City code
	v_y: Variable for y-axis
	png_out: path for png file
	c_cr: Definition of job destruction
	c_den: Definition of denominator when calculating destruction rate
	*/
	
	//local x_min = "9"
	local x_min = "."
	if "`i_city'"=="fips" {
  /*REDACTED 
Code assigning range of x-axis and steps redacted */
		local x_min = "REDACTED"
		local c_xlab = "xlab(REDACTED(REDACTED)REDACTED)"
	}
	else if "`i_city'"=="msa" {
		local x_min = "REDACTED"
		local c_xlab = "xlab(REDACTED(REDACTED)REDACTED)"

	}
	
	local x_max = "."

	if "`c_cr'" == "0" {
/*REDACTED 
Code assigning range of y-axis and steps redacted */
		local y_min = "REDACTED"
		local y_max = "REDACTED"
		local y_step = "REDACTED"
	}
	if "`c_cr'" == "1" {
		local y_min = "REDACTED"
		local y_max = "REDACTED"
		local y_step = "REDACTED"
	}
	if "`c_cr'" == "2" {
		local y_min = "REDACTED"
		local y_max = "REDACTED"
		local y_step = "REDACTED"
	}
	
	
	if 0 {
	twoway (scatter `v_y' ln_emp_city_${year1}, msize(small)), ///
		ylabel(`y_min'(`y_step')`y_max') aspect(.) ///
		yscale(noextend) xscale(noextend) ///
		legend(off) ///
		ytitle("`t_y'") xtitle("ln(City Emp in ${year1})") ///
		title("`t_title'") subtitle("`t_subtitle'")
	if c(mode) != "batch" graph export "${dir_outf}/scat_`png_out'", replace width(3000) height(2000)
	}
	
	lpolyci_data "" "df_fig" "`v_y'" "ln_emp_city_${year1}" "99" "100" "" ""
	
	frame df_fig {
	di "Generate Figures"
	local l_y = "`v_y'"
	
	preserve
	keep `v_y'_x `v_y'_s `v_y'_se
	rounding_4dig "`v_y'_x `v_y'_s `v_y'_se"
	*export excel "${dir_outf}/lpoly_jobcr.xlsx", sheet("`i_city'_`v_y'_cr`c_cr'_d`c_den'") sheetreplace firstrow(var) keepcellfmt
	restore
	
	foreach i_y in `l_y' {
		foreach i_line in s cil ciu x {
			replace `i_y'_`i_line' = . if ! inrange(`i_y'_x, `x_min', `x_max')
		}
		//replace `i_y'_cil = `y_min' if `i_y'_cil <= `y_min'
		//replace `i_y'_ciu = `y_max' if `i_y'_ciu >= `y_max'
	}
	
	if "`c_cr'" == "0" local t_sample = "ALL, JOB{2,4}_Y1_Y2, JOB*_Y1_Y2"
	if "`c_cr'" == "1" local t_sample = "ALL, JOB2_Y1_Y2, JOB*_Y1_Y2"
	if "`c_cr'" == "2" local t_sample = "ALL, JOB4_Y1_Y2, JOB*_Y1_Y2"
	
	local t_city = upper("`i_city'")
	local t_title "Job Destruction"
	if "`c_cr'" == "0" local t_subtitle = "`t_city', Exiting + Shrinking"
	if "`c_cr'" == "1" local t_subtitle = "`t_city', Exiting"
	if "`c_cr'" == "2" local t_subtitle = "`t_city', Shrinking"
	
	twoway (rarea `v_y'_cil `v_y'_ciu `v_y'_x, color(none) fi(inten0) lc(gs8) lw(thin)) ///
		(connected `v_y'_s `v_y'_x, m(i) lcolor(navy) lw(medthick)), ///
		ylabel(`y_min'(`y_step')`y_max') aspect(.) ///
		yscale(noextend) xscale(noextend) ///
		legend(off) ///
		ytitle("`t_y'") xtitle("ln(City Emp in ${year1})") ///
		title("`t_title'") subtitle("`t_subtitle', 99% CI") `c_xlab' note("Sample: `t_sample'")

	if c(mode) != "batch" graph export "${dir_outf}/lploy_`png_out'", replace width(3000) height(2500)
	
	}
end

capture program drop fig_jobcr
program fig_jobcr
	args i_city c_cr c_den
	/*
	i_city: City code
	c_cr: Definition of job destruction
	c_den: Definition of denominator when calculating destruction rate
	*/
	
	//local c_cr = 0
	use ${dir_outf}/city_jobcr, clear
	di "Now runs `i_city' `c_cr' `c_den'"
	keep if city_code == "`i_city'" & cr == `c_cr'
	
	foreach c_plt in 6 { // Use only plant definition 6
	
		preserve
		
		if "`c_den'" == "0" {
			// Use year 1 variable as denominator
			gen emp_den = emp_city_syear1
			gen est_den = est_city_syear1
		}
		else if "`c_den'" == "1" {
			// Use the average of year 1 and year 2 variable as denominator
			gen emp_den = (emp_city_syear1+emp_city_syear2)/2
			gen est_den = (est_city_syear1+est_city_syear2)/2
		}
		else if "`c_den'" == "2" {
			// See below: Use the average of the rates calculated with year 1 and year 2 variables as denominators
			gen emp_den = emp_city_syear1
			gen est_den = est_city_syear1
		}
		
		gen emp_dr = emp_s`c_plt' / emp_den
		gen est_dr = est_s`c_plt' / est_den
		
		if "`c_den'" == "2" {
			capture drop emp_dr
			capture drop est_dr
			
			gen emp_dr = (emp_s`c_plt' / emp_city_syear1 + emp_s`c_plt' / emp_city_syear2) / 2
			gen est_dr = (est_s`c_plt' / est_city_syear1 + est_s`c_plt' / est_city_syear2) / 2
		}
		
		gen ln_emp_dr = ln(abs(emp_dr))
		gen ln_est_dr = ln(abs(est_dr))
		
		// Take average over the 5-year periods and adjustment for 2007-2013 (6 years)
		foreach i_var in emp_dr est_dr {
			replace `i_var' = `i_var' * 5/6 if syear1 == 2007 & syear2 == 2013
		}
		collapse (mean) emp_dr est_dr ln_emp_dr ln_est_dr (first) emp_city_${year1}, by(city jobcr)
		
		gen ln_emp_city_${year1} = ln(emp_city_${year1})
		
		keep if jobcr == 0
		
		save "${dir_outf}/data/`i_city'_cr`c_cr'_plotting_data", replace
		
		fig_lpoly "`i_city'" "emp_dr" "Job Destruction Rate" "city_jobcr_emp_`i_city'_cr`c_cr'_p`c_plt'_den`c_den'_5yr.png" "`c_cr'" "`c_den'"
	
		//fig_lpoly "`i_city'" "est_dr" "Plant Exit Rate" "city_jobcr_est_`i_city'_cr`c_cr'_p`c_plt'_den`c_den'_5yr.png" "`c_cr'" "`c_den'"
		restore
	}
	
end

foreach i_city in msa czone msa1983 {
	//fig_jobcr "`i_city'" "0" "0"
	//fig_jobcr "`i_city'" "1" "0"
	/*
	fig_jobcr "`i_city'" "0" "1"
	fig_jobcr "`i_city'" "1" "1"
	fig_jobcr "`i_city'" "2" "1"
	
	fig_jobcr "`i_city'" "0" "2"
	*/
    
	//fig_jobcr "`i_city'" "1" "2"
	//fig_jobcr "`i_city'" "2" "2"
    
    // Preferred
    fig_jobcr "`i_city'" "1" "1"
	fig_jobcr "`i_city'" "2" "1"
}


di "Ended at $S_DATE $S_TIME"
capture noi log close
// End of do file
